|
|
|
|
|
|
|
The mid-level routines handle the direct calls to the ODBC API and provide simple error checking. The first of the routines allocates an environment handle. This handle is needed before you can attempt to connect to the ODBC interface. |
|
|
|
|
|
|
|
|
Create a new function called AllocateEnv and add the code in Listing 19.6. |
|
|
|
|
|
|
|
|
Listing 19.6. Coding the AllocateEnv function. |
|
|
|
|
|
|
|
|
Private Function AllocateEnv()
'
' Allocates an ODBC environment handle.
' Stores result to hEnv property
'
Dim intResult As Integer
'
intResult = SQLAllocEnv(hEnv)
'
If intResult <> sqlSuccess Then
Err.Raise vbObjectError + 1, App.EXEName, "Cannot allocate environment handle"
End If
'
AllocateEnv = sqlSuccess
'
End Function |
|
|
|
|
|
|
|
|
The routine in Listing 19.6 calls the SQLAllocEnv API and checks for any errors. The SQLAllocEnv API establishes an environment for all ODBC transactions for this session. The hEnv variable that is set here holds a unique number that identifies all transactions that pass from your Visual Basic program to the ODBC interface. |
|
|
|
|
|
|
|
|
Create a new function called Connect, as shown in Listing 19.7. This routine handles the details of completing a connection to the ODBC data source. |
|
|
|
|
|
|
|
|
Listing 19.7. Coding the Connect function. |
|
|
|
|
|
|
|
|
Public Function Connect() As Integer
'
' Allocates and establishes connection
' to DataSource stored in DataSource
' property.
'
Dim intResult As Integer
'
AllocateEnv
' |
|
|
|
|
|